﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Contract.aspx.cs" Inherits="CRMWeb.Pages.Contract" %>

<%@ Register Src="~/UserControls/LoanProfile.ascx" TagPrefix="uc1" TagName="LoanProfile" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <div style="float: left; width: 100%">
        <input type="hidden" id="hdnLender" />
        <input type="hidden" id="hdnEzidebitRefNo" />
        <table style="width: 100%;">
            <tr>
                <td style="vertical-align: top; width: 100%;">
                    <div class="topform_section">
                        <div id="divContactMsg"></div>
                        <div class="firstform_section">
                            <div class="ftitle" style="width:615px">LOAN SETUP</div>
                            <div class="fitem">
                                <label>Account No:</label>
                                <input id="txtLNumber" style="width: 150px;" class="easyui-validatebox" name="LoanNumber" />
                            </div>
                            <div class="fitem">
                                <label>Lender:</label>
                                <input id="txtLender" style="width: 150px;" class="easyui-validatebox" name="LenderName" />
                            </div>
                            <div class="fitem">
                                <label>Contract:</label>
                                <select id="cmbPurpose" class="easyui-combobox" data-options="panelHeight:'auto',editable:false" name="purpose" style="width: 150px;">
                                    <option value="Personal">Personal</option>
                                    <option value="Business">Business</option>
                                </select>
                            </div>
                            <div class="fitem">
                                <label>Facility:</label>
                                <select id="txtProduct" class="easyui-combobox" data-options="panelHeight:'auto',editable:false" name="product" style="width: 180px;">
                                    <option value="Unsecured Business Loan">Unsecured Business Loan</option>
                                    <option value="Business Loan">Business Loan</option>
                                    <option value="Consumer Loan">Consumer Loan</option>
                                    <option value="Costs Recovery">Costs Recovery</option>
                                </select>
                            </div>
                            <div class="fitem">
                                <label>Loan Type:</label>
                                <select id="txtType" class="easyui-combobox" data-options="panelHeight:'auto',editable:false" name="product" style="width: 150px;">
                                    <option value="P And I">P And I</option>
                                    <option value="IO">IO</option>
                                    <option value="FLEX">FLEX</option>
                                </select>
                            </div>
                            <div class="fitem">
                                <label>Interest Method:</label>
                                <select id="txtInterestMethod" class="easyui-combobox" data-options="panelHeight:'auto',editable:false" name="InterestMethod" style="width: 150px;">
                                    <option value="Simple">Simple</option>
                                    <option value="Compound">Compound</option>
                                </select>
                            </div>
                            <div class="ftitle"></div>
                            <div class="fitem">
                                <label>Loan Amount (Principal):</label>
                                <input id="txtLoanAmount" style="width: 150px;" value="0" class="easyui-numberbox" data-options="min:0,precision:2,formatter:parseValue" name="loanamount" />
                            </div>
                            <div class="fitem">
                                <label>Draw Down:</label>
                                <input id="txtDrowDowndate" name="draw_down_date" class="easyui-datebox" data-options="formatter:formatLoanDate" style="width: 90px;" />
                            </div>
                            <div class="fitem">
                                <label>First Payment Date:</label>
                                <input id="txtFirstPaymentDue" name="firtspaymentdue" class="easyui-datebox" data-options="formatter:formatLoanDate" style="width: 90px;" />
                            </div>
                            <div class="fitem">
                                <label>No of repayment:</label>
                                <input id="txtloanterm" style="width: 150px;" class="easyui-numberbox" name="loanterm" />
                            </div>
                            <div class="fitem">
                                <label>Payment Frequency:</label>
                                <input class="easyui-combobox" id="paymentperiod" style="width: 150px;" name="paymentperiod"
                                    data-options="url:'/WebServices/AM/LoanForm.ashx?method=GetAllPaymentPeriod',
				                                    valueField:'PaymentPeriod',
				                                    textField:'PaymentPeriod',
                                                    editable:false,
				                                    panelHeight:'auto'" />
                            </div>
                        </div>
                        <div class="firstform_section">
                            <div class="fitem" style="height:30px">
                                </div>
                            <div class="fitem">
                                <label>Lower Rate (pa):</label>
                                <input id="txtLowerRate" style="width: 150px;" value="0" class="easyui-numberbox" data-options="min:0,precision:2,formatter:parseValue" name="lowerrate" />
                            </div>
                            <div class="fitem">
                                <label>Higher Rate (pa):</label>
                                <input id="txtHigherRate" style="width: 150px;" value="0" class="easyui-numberbox" data-options="min:0,precision:2,formatter:parseValue" name="higherrate" />
                            </div>
                            <div class="fitem">
                                <label>Dishonour Fee:</label>
                                <input id="txtDishonorFee" style="width: 150px;" value="0" class="easyui-numberbox" data-options="min:0,precision:2,formatter:parseValue" name="DishonorFee" />
                            </div>
                            <div class="fitem">
                                <label>Default Notice:</label>
                                <input id="txtDefaultNotice" style="width: 150px;" value="0" class="easyui-numberbox" data-options="min:0,precision:2,formatter:parseValue" name="DefaultNotice" />
                            </div>
                            <div class="ftitle">DDR BANK ACCOUNT</div>
                            <div class="fitem">
                                <label>Account Holder:</label>
                                <input id="AccountHolder2" style="width: 150px;" class="easyui-validatebox" name="AccountHolder2" />
                            </div>
                            <div class="fitem">
                                <label>Bank:</label>
                                <input id="txtBank1" style="width: 150px;" class="easyui-validatebox" name="bankname1" />
                            </div>
                            <div class="fitem">
                                <label>BSB:</label>
                                <input id="txtBSB1" style="width: 150px;" class="easyui-validatebox" name="bsb1" maxlength="7" mask="999-999" validationexpression="^\d{3}-\d{3}$" />
                            </div>
                            <div class="fitem">
                                <label>Account:</label>
                                <input id="txtAccountNo1" style="width: 150px;" class="easyui-validatebox" name="accountno1" maxlength="9" />
                            </div>
                            <div class="fitem">
                                <img src="../../Media/icon_calculator48.jpg" onclick="CalculateRepayment()" style="cursor:pointer;height:25px;" />
                                <label id="txtRepaymentAmount" style="width: auto;" ></label>
                            </div>
                            <div class="fitem" align="center">
                                <span id="lnkbtnContractualPaymentSchedule"><a href="javascript:void(0)" style="width: auto;" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="OpenSchedule()">Payment Schedule</a></span>
                            </div>
                        </div>
                        <div class="firstform_section">
                            <div class="fitem" style="text-align: right;">
                                <span id="lnkbtnSaveLoan"><a id="abtnSaveLoan" href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="SaveLoan()">Save</a></span>
                            </div>
                            <div class="ftitle">Setup DDR Account:</div>
                            <div class="fitem">
                                <table cellpadding="5px" style="width: 100%;">
                                <tr>
                                <td style="text-align: left; width: auto;">
                                <span id="lnkbtnCreateDDRAccount"><a href="javascript:void(0)" style="width: 184px;" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="DDRAccount()">Create DDR Account</a></span>
                                </td>
                                <td style="text-align: left; width: auto;">
                                <label id="txtDDRAccountDate"></label>
                                </td>
                                </tr>
                                </table>
                            </div>
                            <div class="fitem">
                                <table cellpadding="5px" style="width: 100%;">
                                <tr>
                                <td style="text-align: left; width: auto;">
                                <span id="lnkbtnSendPaymentSchedule"><a href="javascript:void(0)" style="width: 184px;" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="PaymentSchDate()">Send Payment Schedule</a></span>
                                </td>
                                <td style="text-align: left; width: auto;">
                                <label id="txtPaymentScheduleDate""></label>
                                </td>
                                </tr>
                                </table>
                            </div>
                            <div class="fitem">
                                <table cellpadding="5px" style="width: 100%;">
                                <tr>
                                <td style="text-align: left; width: auto;">
                                <span id="lnkbtnInputBankAccountDetails"><a href="javascript:void(0)" style="width: 184px;" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="BankAccDate()">Input Bank Account Details</a></span>
                                </td>
                                <td style="text-align: left; width: auto;">
                                <label id="txtBankAccountDate""></label>
                                </td>
                                </tr>
                                </table>
                            </div>
                            <div class="fitem">
                                <table cellpadding="5px" style="width: 100%;">
                                <tr>
                                <td style="text-align: left; width: auto;">
                                <input id="chkDoubleChecked" type="checkbox" onclick="DoubleChecked();" />
                                <label>Double Checked</label>
                                </td>
                                </tr>
                                </table>
                            </div>
                            <div class="fitem">
                                <table cellpadding="5px" style="width: 100%;">
                                <tr>
                                <td style="text-align: left; width: auto;">
                                <span id="lnkbtnCompleted"><a id="abtnCompleted" href="javascript:void(0)" style="width: 184px;" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="EffectiveDate()">Activate Loan</a></span>
                                </td>
                                <td style="text-align: left; width: auto;">
                                <label id="txtEffectiveDate""></label>
                                </td>
                                </tr>
                                </table>
                            </div>
                        </div>
                    </div>
                </td>
            </tr>
        </table>
    </div>
    <script type="text/javascript">
        $(document).ready(function () {
            $('input[name="bsb1"]').mask("999-999");
            DisplayLoan();
            setHeight();
            $('#txtRepaymentAmount').text('Repayment: $0.00 per week');
        });

        function DoubleChecked() {
            if ($('#chkDoubleChecked').is(':checked')) {
                $('#abtnCompleted').linkbutton('enable');
            }
            else {
                $('#abtnCompleted').linkbutton('disable');
            }
        }

        function OpenSchedule() {
            if (IsValidate()) {
                myWindow = window.open("LoanPaymentSchedule.aspx?LoanID=" + getQuerystring('LoanID', '') + "&paymentperiod=" + $('#paymentperiod').combobox('getValue') + "&InterestMethod=" + $('#txtInterestMethod').combobox('getValue') + "&purpose=" + $('#cmbPurpose').combobox('getValue') + "&Product=" + $('#txtProduct').combobox('getValue') + "&Type=" + $('#txtType').combobox('getValue') + "&LoanTerm=" + $('#txtloanterm').numberbox('getValue') + "&LoanAmount=" + $('#txtLoanAmount').numberbox('getValue') + "&LowerRate=" + $('#txtLowerRate').numberbox('getValue') + "&DrowDowndate=" + $('#txtDrowDowndate').datebox('getValue') + "&FirstPaymentDue=" + $('#txtFirstPaymentDue').datebox('getValue'), "", "width=580,height=355,left=400,top=100,resize=no");
                myWindow.focus();
            }
        }

        function IsValidate() {
            var strMsg = '';
            var flag = true;
            if ($('#txtProduct').combobox('getValue') == '')
                strMsg += '<label style="color:red;">Select Facility</label><br/>';
            if ($('#txtType').combobox('getValue') == '')
                strMsg += '<label style="color:red;">Select Loan Type</label><br/>';
            if ($('#txtInterestMethod').combobox('getValue') == '')
                strMsg += '<label style="color:red;">Select Interest Method</label><br/>';
            if ($('#txtDrowDowndate').datebox('getValue') == '')
                strMsg += '<label style="color:red;">Select Drow Down date</label><br/>';
            if ($('#txtFirstPaymentDue').datebox('getValue') == '')
                strMsg += '<label style="color:red;">Select First Payment Due date</label><br/>';
            if ($('#txtLowerRate').numberbox('getValue') == '0' || $('#txtLowerRate').numberbox('getValue') == '')
                strMsg += '<label style="color:red;">Enter Lower Rate</label><br/>';
            if ($('#txtLoanAmount').numberbox('getValue') == '0.00' || $('#txtLoanAmount').numberbox('getValue') == '')
                strMsg += '<label style="color:red;">Enter Loan Amount</label><br/>';
            if ($('#txtloanterm').numberbox('getValue') == '0' || $('#txtloanterm').numberbox('getValue') == '')
                strMsg += '<label style="color:red;">Enter Number of Repayments</label><br/>';
            if ($('#paymentperiod').combobox('getValue') == '')
                strMsg += '<label style="color:red;">Select Payment Frequency</label><br/>';
            var FirstPaymentDue = $('#txtFirstPaymentDue').datebox('getValue');
            if (FirstPaymentDue != '')
                FirstPaymentDue = DefaultformatLoanDate($('#txtFirstPaymentDue').datebox('getValue'));
            var DrowDowndate = $('#txtDrowDowndate').datebox('getValue');
            if (DrowDowndate != '')
                DrowDowndate = DefaultformatLoanDate($('#txtDrowDowndate').datebox('getValue'));
            var frm = new Date(DrowDowndate);
            var frmt = Date.parse(frm);
            var to = new Date(FirstPaymentDue);
            var tot = Date.parse(to);
            if (tot <= frmt)
                strMsg += '<label style="color:red;">FirstPaymentDate is larger then DrowDowndate</label>';

            $('#divContactMsg').html(strMsg);
            if (strMsg == '')
                flag = true;
            else
                flag = false;

            return flag;
        }

        function DefaultformatLoanDate(value) {
            dArr = value.split('/');
            return dArr[1] + '/' + dArr[0] + '/' + dArr[2];
        }

        function formatLoanDate(value, row) {
            if (value == null)
                return value;
            var date = new Date(value);
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            var d = date.getDate();
            return (d < 10 ? ('0' + d) : d) + '/' + (m < 10 ? ('0' + m) : m) + '/' + y;
        }

        function parseValue(value) {
            //alert(value);
            return parseFloat(value);

        }

        /*Update Loan*/
        function SaveLoan() {
            /*Get Text Value*/
            var LNumber = $("#txtLNumber").val();
            var lender = $("#txtLender").val();
            var BankName1 = $('#txtBank1').val();
            var AccountNo1 = $('#txtAccountNo1').val();
            var AccountHolder2 = $('#AccountHolder2').val();

            /*Get Numeric Value*/
            var LoanTerm = $("#txtloanterm").numberbox('getValue');
            var LoanAmount = $("#txtLoanAmount").numberbox('getValue');
            var LowerRate = $("#txtLowerRate").numberbox('getValue');
            var HigherRate = $("#txtHigherRate").numberbox('getValue');
            var DishonorFee = $("#txtDishonorFee").numberbox('getValue');
            var BSB1 = $('#txtBSB1').val();
            var DefaultNotice = $("#txtDefaultNotice").numberbox('getValue');

            /*Get DropDown Value*/
            var paymentperiod = $('#paymentperiod').combobox('getValue');
            var InterestMethod = $("#txtInterestMethod").combobox('getValue');
            var purpose = $("#cmbPurpose").combobox('getValue');
            var Product = $("#txtProduct").combobox('getValue');
            var Type = $("#txtType").combobox('getValue');

            /*Get Date Value*/
            var DrowDowndate = $('#txtDrowDowndate').datebox('getValue');
            if (DrowDowndate != '')
                DrowDowndate = DefaultformatLoanDate($('#txtDrowDowndate').datebox('getValue'));
            var FirstPaymentDue = $('#txtFirstPaymentDue').datebox('getValue');
            if (FirstPaymentDue != '')
                FirstPaymentDue = DefaultformatLoanDate($('#txtFirstPaymentDue').datebox('getValue'));

            /*set value to numeric if it is blank*/
            if (LoanAmount == '')
                LoanAmount = '0';
            if (HigherRate == '')
                HigherRate = '0';
            if (LowerRate == '')
                LowerRate = '0';
            if (DishonorFee == '')
                DishonorFee = '0';
            if (DefaultNotice == '')
                DefaultNotice = '0';

            var todaydate = CurrentDate();

            if (IsValidate()) {
                //insert record into loan table
                $.ajax({
                    url: '/WebServices/AM/LoanForm.ashx?method=UpdateLoanInfo',
                    contentType: 'application/json; charset=utf-8',
                    data: { 'LoanID': getQuerystring('LoanID', ''), 'LoanNumber': LNumber, 'Lender': lender, 'Purpose': purpose, "Product": Product, "Type": Type, "DrawDownDate": DrowDowndate, "LoanTerm": LoanTerm, "FirstPaymentDue": FirstPaymentDue, "PaymetFrequecy": paymentperiod, "LowerRate": LowerRate, "HigherRate": HigherRate, "InterestMethod": InterestMethod, "LoanAmount": LoanAmount, "DishonorFee": DishonorFee, "DefaultNotice": DefaultNotice, "EditDate": todaydate },
                    success: function (data) {
                        if (data == '0') {
                            var strMsg = '';
                            var flag = true;
                            strMsg = '<label style="color:red;">Can not edit record because contract is effective</label>';
                            $('#divContactMsg').html(strMsg);
                            if (strMsg == '')
                                flag = true;
                            else
                                flag = false;
                            return flag;
                        }
                        else {
                            var loanid = getQuerystring('LoanID', '');
                            var LoanType2 = 'DDR BANK ACCOUNT SETUP';
                            var LoanType;

                            //insert record into bank details table DDR Bank Account
                            $.ajax({
                                url: '/WebServices/AM/LoanForm.ashx?method=UpdateBankInfo',
                                contentType: 'application/json; charset=utf-8',
                                data: { "BankName": BankName1, "AccountNo": AccountNo1, "BSB": BSB1, "LoanID": loanid, "LoanType": LoanType2, "AccountHolder": AccountHolder2 },
                                success: function (data) {
                                    //Create Payment Schedule
                                    $.ajax({
                                        url: '/WebServices/AM/LoanForm.ashx?method=AddPaymentSchedule',
                                        contentType: 'application/json; charset=utf-8',
                                        data: { "LoanID": loanid, 'paraPurpose': purpose, "paraProduct": Product, "paraType": Type, "paraSettlementDate": $('#txtDrowDowndate').datebox('getValue'), "paraTerm": LoanTerm, "paraFirstPaymentDate": $('#txtFirstPaymentDue').datebox('getValue'), "paraPaymetFrequecy": paymentperiod, "paraAnnumInterestRate": LowerRate, "paraInterestMethod": InterestMethod, "paraLoanAmount": LoanAmount },
                                        success: function (data) {
                                            if (data == '1') {
                                                alert("Save Data Successfully");
                                            }
                                        }
                                    });
                                }
                            });
                        }
                    }
                });
            }
        }

        function CalculateRepayment() {
            /*Get Numeric Value*/
            var LoanTerm = $("#txtloanterm").numberbox('getValue');
            var LoanAmount = $("#txtLoanAmount").numberbox('getValue');
            var LowerRate = $("#txtLowerRate").numberbox('getValue');

            /*Get DropDown Value*/
            var paymentperiod = $('#paymentperiod').combobox('getValue');
            var InterestMethod = $("#txtInterestMethod").combobox('getValue');
            var purpose = $("#cmbPurpose").combobox('getValue');
            var Product = $("#txtProduct").combobox('getValue');
            var Type = $("#txtType").combobox('getValue');

            /*Get Date Value*/
            var DrowDowndate = $('#txtDrowDowndate').datebox('getValue');
            if (DrowDowndate != '')
                DrowDowndate = DefaultformatLoanDate($('#txtDrowDowndate').datebox('getValue'));
            var FirstPaymentDue = $('#txtFirstPaymentDue').datebox('getValue');
            if (FirstPaymentDue != '')
                FirstPaymentDue = DefaultformatLoanDate($('#txtFirstPaymentDue').datebox('getValue'));

            /*set value to numeric if it is blank*/
            if (LoanAmount == '')
                LoanAmount = '0';
            if (LowerRate == '')
                LowerRate = '0';
            if (IsValidate()) {
                $.ajax({
                    url: '/WebServices/AM/LoanForm.ashx?method=CalculateRepayment',
                    contentType: 'application/json; charset=utf-8',
                    data: { "LoanID": getQuerystring('LoanID', ''), 'paraPurpose': purpose, "paraProduct": Product, "paraType": Type, "paraSettlementDate": $('#txtDrowDowndate').datebox('getValue'), "paraTerm": LoanTerm, "paraFirstPaymentDate": $('#txtFirstPaymentDue').datebox('getValue'), "paraPaymetFrequecy": paymentperiod, "paraAnnumInterestRate": LowerRate, "paraInterestMethod": InterestMethod, "paraLoanAmount": LoanAmount },
                    success: function (data) {
                        $.each(eval(data.replace(/[\r\n]/, '')), function (i, rec) {
                            /* set value in labels */
                            if (i == 0) {
                                var RepaymentAmount = rec.amount;
                                if (rec.PaymetFrequecy == "Weekly")
                                    $('#txtRepaymentAmount').text('Repayment: $' + RepaymentAmount + ' per Week');
                                if (rec.PaymetFrequecy == "Fortnightly")
                                    $('#txtRepaymentAmount').text('Repayment: $' + RepaymentAmount + ' per Fortnightly');
                                if (rec.PaymetFrequecy == "Monthly")
                                    $('#txtRepaymentAmount').text('Repayment: $' + RepaymentAmount + ' per Monthly');
                                if (rec.PaymetFrequecy == "Daily")
                                    $('#txtRepaymentAmount').text('Repayment: $' + RepaymentAmount + ' per Day');
                            }
                        });
                    }
                });
            }
        }

        /*Display Loan */
        function DisplayLoan() {
            $.ajax({
                url: '/WebServices/AM/LoanForm.ashx?method=SearchBankDetailByLoanID',
                contentType: 'application/json; charset=utf-8',
                data: { 'LoanID': getQuerystring('LoanID', '') },
                success: function (data) {
                    $.each(eval(data.replace(/[\r\n]/, '')), function (i, rec) {
                        /* set value in labels */
                        if (rec.LoanType == 'DDR BANK ACCOUNT SETUP') {
                            $('#hdnbankid2').val(rec.bankid);
                            $('#txtBank1').val(rec.bankname);
                            $('#txtBSB1').val(rec.bsb);
                            $('#txtAccountNo1').val(rec.accountno);
                            $('#AccountHolder2').val(rec.HolderName);
                            $('input[name="bsb1"]').mask("999-999");
                        }
                    });
                }
            });

            $.ajax({
                url: '/WebServices/AM/LoanForm.ashx?method=SearchLoanDetailByLoanID',
                contentType: 'application/json; charset=utf-8',
                data: { 'LoanID': getQuerystring('LoanID', '') },
                success: function (data) {
                    $.each(eval(data.replace(/[\r\n]/, '')), function (i, rec) {
                        /* set value in labels */
                        $('#txtLNumber').val(rec.LoanNumber);
                        $('#txtLender').val(rec.Lender);
                        $('#hdnLender').val(rec.Lender);
                        $('#hdnEzidebitRefNo').val(rec.EzidebitRefNo);
                        if (rec.Product != '' && rec.Product != null)
                            $('#txtProduct').combobox('setValue', rec.Product);
                        else
                            $('#txtProduct').combobox('setValue', 'Unsecured Business Loan');
                        if (rec.Type != '' && rec.Type != null)
                            $('#txtType').combobox('setValue', rec.Type);
                        else
                            $('#txtType').combobox('setValue', 'P And I');
                        $('#txtloanterm').numberbox('setValue', rec.LoanTerm);
                        $('#txtDrowDowndate').datebox('setValue', formatLoanDate(rec.DrawDownDate, null));
                        $('#txtFirstPaymentDue').datebox('setValue', formatLoanDate(rec.FirstPaymentDue, null));
                        $('#paymentperiod').combobox('setValue', rec.PaymentPeriod);
                        if (rec.LoanAmount != null)
                            $('#txtLoanAmount').numberbox('setValue', rec.LoanAmount);
                        if (rec.LowerRate != null)
                            $('#txtLowerRate').numberbox('setValue', rec.LowerRate);
                        if (rec.HigherRate != null)
                            $('#txtHigherRate').numberbox('setValue', rec.HigherRate);
                        if (rec.Purpose != '' && rec.Purpose != null)
                            $('#cmbPurpose').combobox('setValue', rec.Purpose);
                        else
                            $('#cmbPurpose').combobox('setValue', 'Personal');
                        if (rec.InterestMethod != '' && rec.InterestMethod != null)
                            $('#txtInterestMethod').combobox('setValue', rec.InterestMethod);
                        else
                            $('#txtInterestMethod').combobox('setValue', 'Simple');
                        if (rec.DishonorFee != null)
                            $('#txtDishonorFee').numberbox('setValue', rec.DishonorFee);
                        if (rec.DefaultNotice != null)
                            $('#txtDefaultNotice').numberbox('setValue', rec.DefaultNotice);
                        if (rec.NewDDRDate != null)
                            $('#txtDDRAccountDate').text('Done On ' + formatLoanDate(rec.NewDDRDate, 0));
                        if (rec.BankAccDate != null)
                            $('#txtBankAccountDate').text('Done On ' + formatLoanDate(rec.BankAccDate, 0));
                        if (rec.PaymentSchDate != null)
                            $('#txtPaymentScheduleDate').text('Done On ' + formatLoanDate(rec.PaymentSchDate, 0));
                        if (rec.EffectiveDate != null)
                            $('#txtEffectiveDate').text('Done On ' + formatLoanDate(rec.EffectiveDate, 0));
                        if (rec.DoubleChecked == '1') {
                            $("#chkDoubleChecked").attr('checked', true);
                            $("#chkDoubleChecked").attr("disabled", true);
                            $('#abtnCompleted').linkbutton('disable');
                            $('#abtnSaveLoan').linkbutton('disable');
                        }
                        else {
                            $('#abtnCompleted').linkbutton('disable');
                        }
                    });
                }
            });
        }

        function GetTerm() {
            var date1 = new Date(DefaultformatLoanDate($('#txtDrowDowndate').datebox('getValue')));
            var date2 = new Date(DefaultformatLoanDate($('#txtMaturity').datebox('getValue')));
            var Term = parseInt(((date2 - date1) / (1000 * 60 * 60 * 24)) / 7);
            $('#txtloanterm').numberbox('setValue', Term);
        }
        $('#txtMaturity').datebox({
            onSelect: function (date) {
                GetTerm();
            }
        });

        function CurrentDate() {
            var currentDate = new Date()
            var day = currentDate.getDate()
            var month = currentDate.getMonth() + 1
            var year = currentDate.getFullYear()
            var todaydate = month + "/" + day + "/" + year;
            return todaydate;
        }

        function DDRAccount() {
            var todaydate = CurrentDate();
            //var LenderName = $("#hdnLender").val();
            var txtSurName = $('#txtSurName').val();
            var txtFirstName = $('#txtFirstName').val();
            var txtStreet = $('#txtStreet').val();
            var txtSuburb = $('#txtSuburb').val();
            var cmbState = $('#txtState').val();
            var txtPostCode = $('#txtPostCode').val();
            var txtEmail1 = $('#txtEmail1').val();
            var txtMobilePhone = $('#txtMobilePhone').val();
                        
            $.ajax({
                url: '/WebServices/AM/LoanForm.ashx?method=UpdateDDRAccount',
                contentType: 'application/json; charset=utf-8',
                data: { "NewDDRDate": todaydate, 'LoanID': getQuerystring('LoanID', ''), "LastName": txtSurName, "FirstName": txtFirstName, "Address1": txtStreet, "Suburb": txtSuburb, "State": cmbState, "PostCode": txtPostCode, "Email": txtEmail1, "MobilePhone": txtMobilePhone },
                success: function (data) {
                    $.each(eval(data.replace(/[\r\n]/, '')), function (i, rec) {
                        $('#hdnEzidebitRefNo').val(rec.EzidebitRefNo);
                        $('#txtDDRAccountDate').text('Done On ' + formatLoanDate(rec.NewDDRDate, 0));
                    });
                    alert("Update Data Successfully");
                }
            });
        }

        function BankAccDate() {
            var todaydate = CurrentDate();
            var digitalKey = '<%=ConfigurationManager.AppSettings["EziDebitDigitalKey"] %>';
            var crRef = $('#hdnEzidebitRefNo').val();
            $.ajax({
                url: '/WebServices/AM/LoanForm.ashx?method=UpdateBankAccDate',
                contentType: 'application/json; charset=utf-8',
                data: { "BankAccDate": todaydate, 'LoanID': getQuerystring('LoanID', '') },
                success: function (data) {
                    $.each(eval(data.replace(/[\r\n]/, '')), function (i, rec) {
                        $('#txtBankAccountDate').text('Done On ' + formatLoanDate(rec.BankAccDate, 0));
                    });
                    if (crRef != '')
                        window.open('https://widget.demo.ezidebit.com.au/account/Edit?dk=' + digitalKey + '&er=' + crRef + '', 'List', 'left = 300,top=150,scrollbars=no, resizable=no,width=500,height=450');
                }
            });
        }

        function PaymentSchDate() {
            var todaydate = CurrentDate();
            var crRef = $('#hdnEzidebitRefNo').val();
            $.ajax({
                url: '/WebServices/AM/LoanForm.ashx?method=UpdatePaymentSchDate',
                contentType: 'application/json; charset=utf-8',
                data: { "PaymentSchDate": todaydate, 'LoanID': getQuerystring('LoanID', ''), 'CustomerRefNo': crRef },
                success: function (data) {
                    $.each(eval(data.replace(/[\r\n]/, '')), function (i, rec) {
                        $('#txtPaymentScheduleDate').text('Done On ' + formatLoanDate(rec.PaymentSchDate, 0));
                    });
                    alert("Update Data Successfully");
                }
            });
        }

        function EffectiveDate() {
            var todaydate = CurrentDate();
            var DoubleChecked = 1;
            var LoanStatus = 2;
            $.ajax({
                url: '/WebServices/AM/LoanForm.ashx?method=UpdateEffectiveDate',
                contentType: 'application/json; charset=utf-8',
                data: { "EffectiveDate": todaydate, 'LoanID': getQuerystring('LoanID', ''), "DoubleChecked": DoubleChecked, "LoanStatus": LoanStatus },
                success: function (data) {
                    if (data == '1') {
                        alert("Update Data Successfully");
                        if (DoubleChecked == '1') {
                            $("#chkDoubleChecked").attr('checked', true);
                            $("#chkDoubleChecked").attr("disabled", true);
                            $('#abtnCompleted').linkbutton('disable');
                            $('#abtnSaveLoan').linkbutton('disable');
                            $('#txtEffectiveDate').text('Done On ' + formatLoanDate(todaydate, 0));
                            $('#cmbLoanStatus').combobox('setValue', LoanStatus);
                        }
                        else {
                            $('#abtnCompleted').linkbutton('disable');
                        }
                    }
                }
            });
        }
    </script>
</body>
</html>
